<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryRef"
      :inline="true"
      v-show="showSearch"
      label-width="68px"
    >
      <el-form-item label="运动名称" prop="sportName">
        <el-input
          v-model="queryParams.sportName"
          placeholder="请输入运动名称"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="创建时间" prop="createTime">
        <el-date-picker
          clearable
          v-model="queryParams.createTime"
          type="date"
          value-format="YYYY-MM-DD"
          placeholder="请选择创建时间"
        >
        </el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery"
          >搜索</el-button
        >
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="Plus"
          @click="handleAdd"
          v-hasPermi="['manage:sport:add']"
          >新增</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="Delete"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['manage:sport:remove']"
          >删除</el-button
        >
      </el-col>
      <!-- <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="Download"
          @click="handleExport"
          v-hasPermi="['manage:sport:export']"
        >导出</el-button>
      </el-col> -->
      <right-toolbar
        v-model:showSearch="showSearch"
        @queryTable="getList"
      ></right-toolbar>
    </el-row>

    <el-tabs v-model="activeTab" @tab-change="handleTabChange">
      <el-tab-pane label="田径类" name="track"></el-tab-pane>
      <el-tab-pane label="跳跃类" name="jump"></el-tab-pane>
    </el-tabs>

    <el-table
      v-loading="loading"
      :data="sportList"
      @selection-change="handleSelectionChange"
      show-overflow-tooltip
    >
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column
        label="序号"
        align="center"
        prop="sportId"
        type="index"
        width="50"
      />
      <el-table-column
        label="运动名称"
        align="center"
        prop="sportName"
        width="auto"
      />
      <el-table-column
        label="运动时长"
        align="center"
        prop="sportDuration"
        width="auto"
      />
      <el-table-column
        label="运动平均心率"
        align="center"
        prop="heartRate"
        width="110"
      />
      <el-table-column
        label="血氧饱和度"
        align="center"
        prop="oxygenSaturation"
        width="100"
      />
      <el-table-column
        label="消耗卡路里"
        align="center"
        prop="sportCalorie"
        width="100"
      />
      <el-table-column
        v-if="activeTab === 'track'"
        label="运动步数"
        align="center"
        prop="stepCount"
        width="auto"
      />
      <el-table-column
        v-if="activeTab === 'track'"
        label="配速"
        align="center"
        prop="sportPace"
        width="auto"
      />
      <el-table-column
        v-if="activeTab === 'track'"
        label="运动距离"
        align="center"
        prop="sportDistance"
        width="auto"
      />
      <el-table-column
        label="有氧时长"
        align="center"
        prop="aerobicDuration"
        width="auto"
      />
      <el-table-column
        label="无氧时长"
        align="center"
        prop="anaerobicDuration"
        width="auto"
      />
      <el-table-column
        v-if="activeTab === 'jump'"
        label="跳跃次数"
        align="center"
        prop="jumpCount"
        width="auto"
      />
      <el-table-column
        label="创建时间"
        align="center"
        prop="createTime"
        width="100"
      >
        <template #default="scope">
          <span>{{ parseTime(scope.row.createTime, "{y}-{m}-{d}") }}</span>
        </template>
      </el-table-column>
      <el-table-column label="备注" align="center" prop="remark" width="200" />
      <el-table-column
        label="操作"
        align="center"
        class-name="small-padding fixed-width"
        width="150"
      >
        <template #default="scope">
          <el-button
            link
            type="primary"
            icon="Edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['manage:sport:edit']"
            >修改</el-button
          >
          <el-button
            link
            type="danger"
            icon="Delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['manage:sport:remove']"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      v-model:page="queryParams.pageNum"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改运动打卡对话框 -->
    <el-dialog :title="title" v-model="open" width="600px" append-to-body>
      <el-tabs v-model="activeTabOpen" type="card">
        <el-tab-pane label="田径类" name="track"></el-tab-pane>
        <el-tab-pane label="跳跃类" name="jump"></el-tab-pane>
      </el-tabs>

      <el-form
        ref="sportRef"
        :model="formTrack"
        :rules="rules"
        label-width="120px"
        v-if="activeTabOpen === 'track'"
      >
        <el-row>
          <el-col :span="12">
            <el-form-item label="运动名称" prop="sportName">
              <el-input
                v-model="formTrack.sportName"
                placeholder="请输入运动名称"
              />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="运动时长" prop="sportDuration">
              <el-input
                v-model="formTrack.sportDuration"
                placeholder="请输入运动时长"
              />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="运动平均心率" prop="heartRate">
              <el-input
                v-model="formTrack.heartRate"
                placeholder="请输入运动平均心率"
              />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="血氧饱和度" prop="oxygenSaturation">
              <el-input
                v-model="formTrack.oxygenSaturation"
                placeholder="请输入血氧饱和度"
              />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="消耗卡路里" prop="sportCalorie">
              <el-input
                v-model="formTrack.sportCalorie"
                placeholder="请输入消耗卡路里"
              />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="运动步数" prop="stepCount">
              <el-input
                v-model="formTrack.stepCount"
                placeholder="请输入运动步数"
              />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="配速" prop="sportPace">
              <el-input
                v-model="formTrack.sportPace"
                placeholder="请输入配速"
              />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="运动距离" prop="sportDistance">
              <el-input
                v-model="formTrack.sportDistance"
                placeholder="请输入运动距离"
              />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="有氧时长" prop="aerobicDuration">
              <el-input
                v-model="formTrack.aerobicDuration"
                placeholder="请输入有氧时长"
              />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="无氧时长" prop="anaerobicDuration">
              <el-input
                v-model="formTrack.anaerobicDuration"
                placeholder="请输入无氧时长"
              />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="24">
            <el-form-item label="备注" prop="remark">
              <el-input
                type="textarea"
                v-model="formTrack.remark"
                placeholder="请输入备注"
              />
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <el-form
        ref="sportRef"
        :model="formJump"
        :rules="rules"
        label-width="120px"
        v-if="activeTabOpen === 'jump'"
      >
        <el-row>
          <el-col :span="12">
            <el-form-item label="运动名称" prop="sportName">
              <el-input
                v-model="formJump.sportName"
                placeholder="请输入运动名称"
              />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="运动时长" prop="sportDuration">
              <el-input
                v-model="formJump.sportDuration"
                placeholder="请输入运动时长"
              />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="运动平均心率" prop="heartRate">
              <el-input
                v-model="formJump.heartRate"
                placeholder="请输入运动平均心率"
              />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="血氧饱和度" prop="oxygenSaturation">
              <el-input
                v-model="formJump.oxygenSaturation"
                placeholder="请输入血氧饱和度"
              />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="消耗卡路里" prop="sportCalorie">
              <el-input
                v-model="formJump.sportCalorie"
                placeholder="请输入消耗卡路里"
              />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="跳跃次数" prop="jumpCount">
              <el-input
                v-model="formJump.jumpCount"
                placeholder="请输入跳跃次数"
              />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="有氧时长" prop="aerobicDuration">
              <el-input
                v-model="formJump.aerobicDuration"
                placeholder="请输入有氧时长"
              />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="无氧时长" prop="anaerobicDuration">
              <el-input
                v-model="formJump.anaerobicDuration"
                placeholder="请输入无氧时长"
              />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="24">
            <el-form-item label="备注" prop="remark">
              <el-input
                type="textarea"
                v-model="formJump.remark"
                placeholder="请输入备注"
              />
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup name="Sport">
import { listSport, getSport, delSport, addSport, updateSport } from "@/api/manage/sport"

const { proxy } = getCurrentInstance()

const sportList = ref([])
const open = ref(false)
const loading = ref(true)
const showSearch = ref(true)
const ids = ref([])
const single = ref(true)
const multiple = ref(true)
const total = ref(0)
const title = ref("")

const activeTab = ref('track') // 默认选中田径类
const activeTabOpen = ref('track') // 默认选中田径类

const data = reactive({
  formTrack: {},
  formJump: {},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    sportName: null,
    createTime: null,
    sportType: 'track'
  },
  rules: {
    sportName: [{ required: true, message: "运动名称不能为空", trigger: "blur" }],
    sportDuration: [{ required: true, message: "运动时长不能为空", trigger: "blur" }],
    heartRate: [{ required: true, message: "运动平均心率不能为空", trigger: "blur" }],
    oxygenSaturation: [{ required: true, message: "血氧饱和度不能为空", trigger: "blur" }],
    sportCalorie: [{ required: true, message: "消耗卡路里不能为空", trigger: "blur" }],
    aerobicDuration: [{ required: true, message: "有氧时长不能为空", trigger: "blur" }],
    anaerobicDuration: [{ required: true, message: "无氧时长不能为空", trigger: "blur" }],
    // 田径类特有字段
    stepCount: [{ required: true, message: "运动步数不能为空", trigger: "blur" }],
    sportPace: [{ required: true, message: "配速不能为空", trigger: "blur" }],
    sportDistance: [{ required: true, message: "运动距离不能为空", trigger: "blur" }],
    // 跳跃类特有字段
    jumpCount: [{ required: true, message: "跳跃次数不能为空", trigger: "blur" }]
  }
})

const { queryParams, formTrack, formJump, rules } = toRefs(data)

/** 查询运动打卡列表 */
function getList () {
  loading.value = true
  if (activeTab === 'track') {
    queryParams.value.sportType = 'track'
  } else if (activeTab === 'jump') {
    queryParams.value.sportType = 'jump'
  }
  listSport(queryParams.value).then(response => {
    sportList.value = response.rows
    total.value = response.total
    loading.value = false
  })
}

// 取消按钮
function cancel () {
  open.value = false
  reset()
}

// 表单重置
function reset () {
  formTrack.value = {
    sportId: null,
    sportName: null,
    sportDuration: null,
    heartRate: null,
    oxygenSaturation: null,
    sportCalorie: null,
    stepCount: null,
    sportPace: null,
    sportDistance: null,
    aerobicDuration: null,
    anaerobicDuration: null,
    jumpCount: null,
    createTime: null,
    remark: null
  }
  formJump.value = {
    sportId: null,
    sportName: null,
    sportDuration: null,
    heartRate: null,
    oxygenSaturation: null,
    sportCalorie: null,
    aerobicDuration: null,
    anaerobicDuration: null,
    jumpCount: null,
    createTime: null,
    remark: null
  }
  proxy.resetForm("sportRef")
}

/** 搜索按钮操作 */
function handleQuery () {
  queryParams.value.pageNum = 1
  getList()
}

/** 重置按钮操作 */
function resetQuery () {
  proxy.resetForm("queryRef")
  handleQuery()
}

// 多选框选中数据
function handleSelectionChange (selection) {
  ids.value = selection.map(item => item.sportId)
  single.value = selection.length != 1
  multiple.value = !selection.length
}

/** 新增按钮操作 */
function handleAdd () {
  reset()
  open.value = true
  title.value = "添加运动打卡"
}

/** 修改按钮操作 */
function handleUpdate (row) {
  reset()
  const _sportId = row.sportId || ids.value
  getSport(_sportId).then(response => {
    if (activeTab.value === 'track') {
      formTrack.value = response.data
    }
    if (activeTab.value === 'jump') {
      activeTabOpen.value = 'jump'
      formJump.value = response.data
    }
    // form.value = response.data
    open.value = true
    title.value = "修改运动打卡"
  })
}

/** 提交按钮 */
function submitForm () {
  proxy.$refs["sportRef"].validate(valid => {
    if (valid) {
      let formData = {}
      if (activeTabOpen.value === 'track') {
        formData = formTrack.value
        formData.sportType = 'track'
      } else if (activeTabOpen.value === 'jump') {
        formData = formJump.value
        formData.sportType = 'jump'
      }

      if (formData.sportId != null) {
        updateSport(formData).then(response => {
          proxy.$modal.msgSuccess("修改成功")
          open.value = false
          getList()
        })
      } else {
        // console.log("新增运动数据", formData)
        addSport(formData).then(response => {
          proxy.$modal.msgSuccess("新增成功")
          open.value = false
          getList()
        })
      }
    }
  })
}

/** 删除按钮操作 */
function handleDelete (row) {
  const _sportIds = row.sportId || ids.value
  const delName = _sportIds.length >= 1 ? '选中的运动打卡记录' : row.sportName
  // 将日期转化为xx年xx月xx日的格式
  const dateStr = new Date(row.createTime).toLocaleDateString()
  let str = dateStr.split('/')
  const date = str[0] + '年' + str[1] + '月' + str[2] + '日'
  proxy.$modal.confirm('确定要删除' + date + '的' + delName + '记录吗？').then(function () {
    return delSport(_sportIds)
  }).then(() => {
    getList()
    proxy.$modal.msgSuccess("删除成功")
  }).catch(() => { })
}

/** 导出按钮操作 */
function handleExport () {
  proxy.download('manage/sport/export', {
    ...queryParams.value
  }, `sport_${new Date().getTime()}.xlsx`)
}

// 切换tab操作
function handleTabChange () {
  // 根据当前选中的tab设置sportType
  // console.log('activeTab.value', activeTab.value)
  if (activeTab.value === 'track') {
    queryParams.value.sportType = 'track'
  } else if (activeTab.value === 'jump') {
    queryParams.value.sportType = 'jump'
  }
  // 重置分页参数
  queryParams.value.pageNum = 1
  // console.log('queryParams.value', queryParams.value)
  // 重新获取数据
  getList()
}

// 页面初始化时调用
getList();
</script>
